package com.hydong.novatest.activity

import android.content.res.Configuration
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import androidx.core.content.ContextCompat
import androidx.fragment.app.FragmentManager
import androidx.fragment.app.FragmentPagerAdapter
import com.hydong.novatest.R
import com.hydong.novatest.Utils.QDSkinManager
import com.hydong.novatest.base.BaseActivity
import com.hydong.novatest.base.BaseFragment
import com.hydong.novatest.fragment.ForumFragment
import com.hydong.novatest.fragment.HomeFragment
import com.hydong.novatest.fragment.MineFragment
import com.qmuiteam.qmui.skin.QMUISkinManager
import com.qmuiteam.qmui.util.QMUIDisplayHelper
import com.qmuiteam.qmui.util.QMUIStatusBarHelper
import com.qmuiteam.qmui.widget.dialog.QMUIDialog
import com.qmuiteam.qmui.widget.tab.QMUITabBuilder
import kotlinx.android.synthetic.main.activity_tab.*

class TabActivity : BaseActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        val skinManager = QMUISkinManager.defaultInstance(this)
        setSkinManager(skinManager)
        mOnSkinChangeListener.onSkinChange(skinManager, -1, skinManager.currentSkin)

        val root: View = LayoutInflater.from(this).inflate(R.layout.activity_tab, null)
        setContentView(root)

        initView()
    }

    private fun initView() {
        // tabs
        initTabs()
        // pager
        initPagers()

        fab?.setOnClickListener {
            val items = arrayOf("蓝色（默认）", "黑色", "白色")
            QMUIDialog.MenuDialogBuilder(this)
                .addItems(items) { dialog, which ->
                    QDSkinManager.changeSkin(which + 1)
                    dialog.dismiss()
                }
                .setSkinManager(QMUISkinManager.defaultInstance(this))
                .create()
                .show()
        }
    }

    private fun initTabs() {
        val builder: QMUITabBuilder = tabs.tabBuilder()
//        builder.setTypeface(null, Typeface.DEFAULT_BOLD)
//        builder.setSelectedIconScale(1.2f)
        builder.setTextSize(QMUIDisplayHelper.sp2px(applicationContext, 13), QMUIDisplayHelper.sp2px(applicationContext, 13))
//        builder.setDynamicChangeIconColor(false)

        val component = builder
            .setNormalDrawable(ContextCompat.getDrawable(applicationContext, R.mipmap.icon_tabbar_component))
            .setSelectedDrawable(ContextCompat.getDrawable(applicationContext, R.mipmap.icon_tabbar_component_selected))
            .setText("设备")
            .build(applicationContext)
        val util = builder
            .setNormalDrawable(ContextCompat.getDrawable(applicationContext, R.mipmap.icon_tabbar_util))
            .setSelectedDrawable(ContextCompat.getDrawable(applicationContext, R.mipmap.icon_tabbar_util_selected))
            .setText("社区")
            .build(applicationContext)
        val lab = builder
            .setNormalDrawable(ContextCompat.getDrawable(applicationContext, R.mipmap.icon_tabbar_lab))
            .setSelectedDrawable(ContextCompat.getDrawable(applicationContext, R.mipmap.icon_tabbar_lab_selected))
            .setText("我的")
            .build(applicationContext)

        tabs.addTab(component)
            .addTab(util)
            .addTab(lab)

        // 小红点
        val tab = tabs.getTab(2)
        tab.setRedPoint()
        tab.signCount = 2
    }

    private fun initPagers() {
        val list: MutableList<BaseFragment> = ArrayList()
        list.add(HomeFragment())
        list.add(ForumFragment())
        list.add(MineFragment())
        pager?.adapter = ViewPagerAdapter(supportFragmentManager, list)

        tabs.setupWithViewPager(pager, false);
    }

    private class ViewPagerAdapter(fm: FragmentManager, private val list: List<BaseFragment>) : FragmentPagerAdapter(fm) {
        override fun getItem(position: Int): BaseFragment {
            return list[position]
        }

        override fun getCount(): Int {
            return list.size
        }
    }

    override fun onConfigurationChanged(newConfig: Configuration) {
        super.onConfigurationChanged(newConfig)
    }

    override fun onStart() {
        super.onStart()
        skinManager?.addSkinChangeListener(mOnSkinChangeListener)
    }

    override fun onStop() {
        super.onStop()
        skinManager?.removeSkinChangeListener(mOnSkinChangeListener)
    }

    private val mOnSkinChangeListener = QMUISkinManager.OnSkinChangeListener { skinManager, oldSkin, newSkin ->
        if (newSkin == QDSkinManager.SKIN_WHITE) {
            QMUIStatusBarHelper.setStatusBarLightMode(this)
        } else {
            QMUIStatusBarHelper.setStatusBarDarkMode(this)
        }
    }
}